<template>
	<view class="font content">
		<view v-if="type==1">
			<jinEdit placeholder="请输入内容" @editOk="editOk" :uploadFileUrl="url+'/PictureController/fileUpload'" ></jinEdit> 
		</view>
		<view v-else-if="type == 2">
			<view class="card" style="background-color: white;">
				<label>请输入跳转链接:</label>
				<input type="text" v-model="jump.skipLinks" style="margin: 20rpx;"></input>
				<label>请输入跳转提示语:</label>
				<input type="text" v-model="jump.clues" style="margin: 20rpx;"/>
			</view>
			<button class="sure" @click="jumpPage">确认</button>
			<button class="back" @click="back">返回</button>
		</view>
		<view v-else-if="type == 3">
			<view class="card">
				<view class="uni-list">
					<view class="uni-list-cell uni-list-cell-pd">
						<view class="uni-list-cell-db">允许分享:</view>
						<switch :checked="share.isAllowedToShare" @change="AllowedToShare" />
					</view>
					<view class="uni-list-cell uni-list-cell-pd" v-show="share.isAllowedToShare">
						<view class="uni-list-cell-db">微信分享正标题:</view>
						<input type="text" v-model="share.isTheTitle">
					</view>
					<view class="uni-list-cell uni-list-cell-pd" v-show="share.isAllowedToShare">
						<view class="uni-list-cell-db">微信分享副标题:</view>
						<input type="text" v-model="share.subtitle">
					</view>
					<view class="uni-list-cell uni-list-cell-pd" v-show="share.isAllowedToShare">
						<view class="uni-list-cell-db">微信分享图:</view>
						<button class="uploadImg" @click="uploadImg">上传图片</button>
					</view>
				</view>
			</view>
			<button class="sure" @click="Share">确认</button>
			<button class="back" @click="back">返回</button>
		</view>
	</view>
</template>

<script>
	import jinEdit from '@/components/common/jin-edit/jin-edit.vue';
import { url } from '../../uni_modules/uv-ui-tools/libs/function/test';
export default {
	data() {
		return {
			url:this.url,
			formid:"",
			type:0,
			content:"",
			jump:{
				skipLinks:"",
				clues:"",
				handling:true
			},
			share:{
				isTheTitle:"",
				subtitle:"",
				isAllowedToShare:false,
				shareWithTheQuery:true,
				imgUrl:'https://img-baofun.zhhainiao.com/fs/6f491f705a299b3fdd571aa5b095058a.jpg'
			},
			
		}
	},
	components: {
		jinEdit
	},
	onLoad:function(option){
		this.type = +option.type
		this.formid = option.formid
		this.$request('/QuestionnaireSettingController/GetSettingsSharing','GET',{formid:this.formid})
		.then(res=>{
			console.log(res);
			this.share['isTheTitle'] = res.data.data.is_the_title
			this.share['isAllowedToShare'] = res.data.data.is_allowed_to_share
			this.share['imgUrl'] = res.data.data.img_url
			this.share['subtitle'] = res.data.data.subtitle
		})
	},
	methods: {
		back(){
			uni.navigateBack()
		},
		prevPage(){
			var pages = getCurrentPages();
			var prevPage = pages[pages.length - 2];
			return prevPage
		},
		// 设置分享
		Share(){
			let data = this.share
			data['formid'] = this.formid
			this.$request('/QuestionnaireSettingController/SetupSharing','POST',data)
			.then(res=>{
				console.log(res);
				this.prevPage().setData({
				        shareWithTheQuery:true
				})
				uni.navigateBack()
			})
		},
		// 跳转链接
		jumpPage(){
			if(!url(this.jump.skipLinks)){
				uni.showModal({
					title:"系统提示",
					content:"链接格式不正确"
				})
				return
			}
			else if(!this.jump.clues){
				uni.showModal({
					title:"系统提示",
					content:"请填写跳转提示"
				})
				return
			}
			let data = {
				content:JSON.stringify({skipLinks:this.skipLinks,clues:this.clues}),
				formid:this.formid,
				handling:true,
				type:2
			}
			this.$request('/QuestionnaireSettingController/handlingWayAfterSummit','POST',data)
			.then(res=>{
				console.log(res);
				this.prevPage().setData({
				        handling:true
				})
				uni.navigateBack()
			})
		},
	// 点击完成自定义图文内容
		editOk(res) {
			this.content = JSON.stringify({
				html:res.html,
				backgroundColor:res.backgroundColor,
				fontColor:res.fontColor
			})
			let data = {
				content:this.content,
				formid:this.formid,
				type:this.type,
				handling:true
			}
			console.log(data);
			this.$request('/QuestionnaireSettingController/handlingWayAfterSummit','POST',data)
			.then(res=>{
				if(res.data.code == 200){
					this.prevPage().setData({
							handling:true
					})
					uni.navigateBack()
				}
			})
		},
		AllowedToShare(e){
			this.share.isAllowedToShare = e.detail.value
		},
		uploadImg(){
			uni.chooseImage({
				count: 1, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album', 'camera'], //从相册选择
				success: async(res) => {
					var tempFilePaths = res.tempFilePaths;
					uni.showLoading({
						title: '正在上传中...'
					})
					for (let temp of tempFilePaths) {
						// 图片上传服务器
						await uni.uploadFile({
							url: 'http://192.168.0.102:8083'+'/PictureController/fileUpload',
							filePath: temp,
							name: 'file',
							formData:{
								'formid':this.formid,
								'PhotoType':4
							},
							success: res => {
								console.log(res);
								this.share.imgUrl = JSON.parse(res.data).data.imageLink
								// 上传完成后处理
								uni.hideLoading()
							},
							fail: (err) => {
								console.log(err);
							}
						});
					}
				}
			});
		}
	}
}
</script>
	
<style lang="less" scoped>
	page{
		background-color: #f5f5f5;
	}
	.card{
		margin: 30rpx;
		input{
			border: 1px solid gray;
		}
		label{
			margin-top: 20rpx;
		}
	}
	.sure{
		width: 30%;
		background-color: #80b156;
		color: white;
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translate(-50%,-180%);
	}
	.back{
		width: 30%;
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translate(-50%,-50%);
	}
	.uploadImg{
		background-color: #80b156;
		color: white;
		border-radius: 10px;
		width: 30%;
		font-size: 12px;
	}
</style>